package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveFieldSvc;
import com.kmxd.ams.client.archive.IArchiveRelationSettingSvc;
import com.kmxd.ams.client.archive.IArchiveTypeSvc;
import com.kmxd.ams.client.archive.dto.ArchiveRelationSettingAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveRelationSettingEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveRelationSettingQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveRelationSettingFieldVO;
import com.kmxd.ams.client.archive.vo.ArchiveRelationSettingListVO;
import com.kmxd.ams.client.archive.vo.ArchiveRelationSettingVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.ConvertUtil;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveField;
import com.kmxd.ams.infra.archive.entity.ArchiveRelationSetting;
import com.kmxd.ams.infra.archive.entity.ArchiveType;
import com.kmxd.ams.infra.archive.mapper.ArchiveRelationSettingMapper;
import com.kmxd.ams.infra.tmpl.dto.ArchTmplBO;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 门类关联设置 服务实现类
 *
 * @author nickbi
 * @since 2022-07-20
 */
@Service
@AllArgsConstructor
public class ArchiveRelationSettingSvcImpl
    extends ServiceImpl<ArchiveRelationSettingMapper, ArchiveRelationSetting>
    implements IArchiveRelationSettingSvc {

  private ArchiveRelationSettingMapper archiveRelationSettingMapper;
  private IArchiveFieldSvc archiveFieldSvc;
  private IArchiveTypeSvc archiveTypeSvc;

  @Override
  public Page<ArchiveRelationSettingListVO> selectPage(
      Page reqPage, ArchiveRelationSettingQueryDTO req) {

    LambdaQueryWrapper<ArchiveRelationSetting> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveRelationSetting>(), req).lambda();
    Page<ArchiveRelationSetting> page =
        archiveRelationSettingMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveRelationSettingListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveRelationSettingListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveRelationSettingListVO> selectList(ArchiveRelationSettingQueryDTO req) {

    LambdaQueryWrapper<ArchiveRelationSetting> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveRelationSetting>(), req).lambda();
    List<ArchiveRelationSetting> list = archiveRelationSettingMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveRelationSettingListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveRelationSettingAddDTO req) {
    ArchiveRelationSetting entity = BeanUtil.copyProperties(req, ArchiveRelationSetting.class);
    archiveRelationSettingMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveRelationSettingEditDTO req) {
    ArchiveRelationSetting entity = BeanUtil.copyProperties(req, ArchiveRelationSetting.class);
    archiveRelationSettingMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveRelationSettingVO view(ArchiveRelationSettingQueryDTO req) {
    LambdaQueryWrapper<ArchiveRelationSetting> wrapper = Wrappers.lambdaQuery();
    wrapper.eq(null != req.getId(), ArchiveRelationSetting::getId, req.getId());
    if (null != req.getArchTypeId()) {
      wrapper.eq(ArchiveRelationSetting::getArchTypeId, req.getArchTypeId());
      wrapper.eq(ArchiveRelationSetting::getTableId, req.getTableId());
    }

    ArchiveRelationSetting entity = archiveRelationSettingMapper.selectOne(wrapper);
    if (null == entity) {
      return null;
    }
    ArchiveRelationSettingVO vo = BeanUtil.copyProperties(entity, ArchiveRelationSettingVO.class);
    // 选择了元数据信息
    if (vo.getRelationField() && StringUtils.isNotBlank(vo.getFieldIds())) {
      List<ArchiveField> archiveFields = archiveFieldSvc.listByIds(IdUtil.split(vo.getFieldIds()));
      List<ArchiveRelationSettingFieldVO> fieldVOList =
          archiveFields.stream()
              .map(
                  f -> {
                    ArchiveRelationSettingFieldVO field =
                        ConvertUtil.toBean(f, ArchiveRelationSettingFieldVO.class);
                    field.setFieldId(field.getId());
                    ArchiveType archiveType =
                        Optional.ofNullable(archiveTypeSvc.getById(field.getArchTypeId()))
                            .orElse(new ArchiveType());
                    field.setArchTypeName(archiveType.getName());
                    field.setArchAndFieldName(
                        StrUtil.format(
                            "{}-{}-{}",
                            archiveType.getName(),
                            ArchTmplBO.getTableDescription(field.getTableId()),
                            field.getDbFieldName()));
                    return field;
                  })
              .collect(Collectors.toList());
      vo.setFieldList(fieldVOList);
    }
    return vo;
  }
}
